package com.seecen.cywbackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seecen.cywbackend.mapper.GroupMapper;
import com.seecen.cywbackend.pojo.Follows;
import com.seecen.cywbackend.service.FollowsService;
import com.seecen.cywbackend.mapper.FollowsMapper;
import com.seecen.cywbackend.utils.Result;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

/**
* @author lenovo
* @description 针对表【t_follows(用户关注（圈子/圈子消息）表)】的数据库操作Service实现
* @createDate 2025-03-21 17:50:13
*/
@Service
public class FollowsServiceImpl extends ServiceImpl<FollowsMapper, Follows>
    implements FollowsService{
    @Resource
    private FollowsMapper followsMapper;
    @Resource
    private GroupMapper groupMapper;

    @Override
    public Result followUser(Long targetId, Long userId) {
        LambdaQueryWrapper<Follows> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Follows::getUserId,userId)
                .eq(Follows::getTargetId, targetId)
                .eq(Follows::getTargetType, 1);
        if(followsMapper.selectOne(wrapper) == null){
            Follows follows = new Follows();
            follows.setUserId(userId);
            follows.setTargetId(targetId);
            follows.setTargetType(1);
            followsMapper.insert(follows);
        }else{
            followsMapper.delete(wrapper);
        }
        return Result.ok(null);
    }

    @Override
    public Result followGroup(Integer groupId, Long userId) {
        LambdaQueryWrapper<Follows> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Follows::getUserId,userId)
                .eq(Follows::getTargetId, groupId)
                .eq(Follows::getTargetType, 2);
        if(followsMapper.selectOne(wrapper) == null){
            Follows follows = new Follows();
            follows.setUserId(userId);
            follows.setTargetId(Long.valueOf(groupId));
            follows.setTargetType(2);
            followsMapper.insert(follows);
            groupMapper.incrementMemberCount(groupId);
        }else{
            followsMapper.delete(wrapper);
            groupMapper.decrementMemberCount(groupId);
        }
        return Result.ok(null);
    }
}




